package sfeir.ergosum.tools;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import sfeir.ergosum.model.spreadsheet.SpreadsheetEntry;

public class SpreadsheetFeedParser {
    private static XmlPullParserFactory factory = null;
    {
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }

    private XmlPullParser xpp;

    public SpreadsheetFeedParser(Reader in) {
        try {
            xpp = factory.newPullParser();
            xpp.setInput(in);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }

    public SpreadsheetFeedParser(String in) {
        this(new StringReader(in));
    }

    public List<SpreadsheetEntry> parseSpreadsheetList(String email) {
        List<SpreadsheetEntry> entries = new ArrayList<SpreadsheetEntry>();
        SpreadsheetEntry current = null;
        try {
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
//                    Log.w("Parser", xpp.getName());
                    if ("entry".equals(xpp.getName())) {
                        current = new SpreadsheetEntry();
                    } else if ("email".equals(xpp.getName())) {
                        current.setEmail(xpp.nextText());
                    } else if (null != current && "id".equals(xpp.getName())) {
                        current.setId(xpp.nextText());
                    } else if (null != current && "title".equals(xpp.getName())) {
                        current.setTitle(xpp.nextText());
                    }
                } else if (eventType == XmlPullParser.END_TAG
                        && "entry".equals(xpp.getName()))
                    if (current.getEmail().equals(email))
                        entries.add(current);
                eventType = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return entries;
    }

    public SpreadsheetEntry parseNewSpreadsheet() {
        SpreadsheetEntry entry = null;
        try {
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if ("entry".equals(xpp.getName())) {
                        entry = new SpreadsheetEntry();
                    } else if ("email".equals(xpp.getName())) {
                        entry.setEmail(xpp.nextText());
                    } else if (null != entry && "id".equals(xpp.getName())) {
                        entry.setId(xpp.nextText());
                    } else if (null != entry && "title".equals(xpp.getName())) {
                        entry.setTitle(xpp.nextText());
                    }
                } else if (eventType == XmlPullParser.END_TAG
                        && "entry".equals(xpp.getName()))
                        return entry;
                eventType = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return entry;
    }
}
